package com.atom.module.mvvm.adapter

import android.text.InputType
import android.widget.EditText
import android.widget.TextView
import androidx.databinding.BindingAdapter
import androidx.databinding.ObservableBoolean

/**
 * View上布局文件的参数进行适配
 * 当展示在布局前,系统会根据布局文件的每个View的自定义参数,来寻找自定义参数对应的适配器
 */
object TextViewAttrAdapter {
    /**
     * 点击事件适配器,该参数和View的OnClick不冲突,该参数的优先级低于OnClick的优先级
     * 点击事件的适配器,能够对此进行监听
     */
    @BindingAdapter(value = ["event_watcher_text"], requireAll = true)
    @JvmStatic
    fun eventOnClick(v: TextView, watcherEvent: TextWatcherEvent) {
        v.addTextChangedListener(watcherEvent)
    }


    @BindingAdapter(value = ["password_visible"], requireAll = true)
    @JvmStatic
    fun setPasswordVisible(v: TextView, isVisible: ObservableBoolean) {
        var inputType = InputType.TYPE_CLASS_TEXT or InputType.TYPE_TEXT_VARIATION_PASSWORD
        if (isVisible.get()) {
            inputType = InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD
        }
        v.inputType = inputType;
        // 保持光标位置
        if (v is EditText) {
            v.setSelection(v.text?.length ?: 0)
        }
    }
}
